package com.tt.fashionWebSite.service.imp;

import com.tt.fashionWebSite.dao.CapBacDao;
import com.tt.fashionWebSite.dao.PhanQuyenDao;
import com.tt.fashionWebSite.dao.ThongTinThanhVienDao;
import com.tt.fashionWebSite.domain.*;
import com.tt.fashionWebSite.dto.*;
import com.tt.fashionWebSite.service.Ad_MemberAccountService;
import com.tt.fashionWebSite.service.GenericService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("ad_memberAccountService")
public class Ad_MemberAccountServiceImp extends GenericService implements Ad_MemberAccountService {
    @Autowired
    @Qualifier("phanQuyen")
    private PhanQuyenDao phanQuyenDao;

    @Autowired
    @Qualifier("capBac")
    private CapBacDao capBacDao;

    @Autowired
    @Qualifier("thongTinThanhVien")
    private ThongTinThanhVienDao thongTinThanhVienDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int adminMemberAccount(PhanQuyenDto accountDto) {
        PhanQuyen phanQuyen = phanQuyenDao.findById(accountDto.getThongTinThanhVien().getMaTV());
        if(phanQuyen == null){
            phanQuyen = new PhanQuyen();
            ThongTinThanhVien thongTinThanhVien = thongTinThanhVienDao.findById(accountDto.getThongTinThanhVien().getMaTV());
            phanQuyen.setThongTinThanhVien(thongTinThanhVien);
            phanQuyen.setMatKhau(accountDto.getMatKhau());
            CapBac capBac = capBacDao.findById(accountDto.getCapBac().getMaCB());
            phanQuyen.setCapBac(capBac);
            phanQuyenDao.insert(phanQuyen);
            return JsonResponseCode.SUCCESS;
        }
        else {
            return JsonResponseCode.FAILED;
        }
    }

    @Override
    public List<CapBacDto> adminGetMemberLevel () {
        return mapper.mapCollection(capBacDao.getInfoRegister(), CapBacDto.class);
    }

    @Override
    public List<PhanQuyenDto> adminGetAllAccount () {
        return mapper.mapCollection(phanQuyenDao.getAllAccount(), PhanQuyenDto.class);
    }

    @Override
    public List<ThongTinThanhVienDto> getAllMemberCode () {
        return mapper.mapCollection(thongTinThanhVienDao.getMemberInfor(), ThongTinThanhVienDto.class);
    }

    public PhanQuyenDto getMemberAccountInfoEdit(PhanQuyenDto accountDto){
        PhanQuyen phanQuyen = phanQuyenDao.findById(accountDto.getThongTinThanhVien().getMaTV());
        PhanQuyenDto phanQuyenDto = mapper.map(phanQuyen, PhanQuyenDto.class,PHAN_QUYEN_MAP_ID);
        return  phanQuyenDto;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public PhanQuyenDto registerMemberAccountEdit(PhanQuyenDto accountDto) {
        PhanQuyen phanQuyen = phanQuyenDao.findById(accountDto.getThongTinThanhVien().getMaTV());
        try {
            ThongTinThanhVien thongTinThanhVien = thongTinThanhVienDao.findById(accountDto.getThongTinThanhVien().getMaTV());
            phanQuyen.setThongTinThanhVien(thongTinThanhVien);

            phanQuyen.setMatKhau(accountDto.getMatKhau());

            CapBac capBac = capBacDao.findById(accountDto.getCapBac().getMaCB());
            phanQuyen.setCapBac(capBac);

            phanQuyenDao.update(phanQuyen);
            return accountDto;
        }
        catch (Exception ex) {
            return null;
        }
    }
}
